package cn.net.lwj.sjqy.init;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.persistence.Column;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.StringUtils;

/**
 * @Author: WLJ
 * @Description:
 * @Date: Created in 9:38 2021/3/17
 * @Modified By: lwj  以前的无效...新增实现BeanPostProcessor类,使本类在mybatis初始化前初始化,达到修改pojo注解@Column.name的值,以适配kingbase8数据库保留字
 */
//@AutoConfigureBefore(org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.class)
//@AutoConfigureBefore({MybatisAutoConfiguration.class, DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class,DataSourceAutoConfiguration.class })
//@ConditionalOnProperty(name = "cluster",havingValue = "kingbase")
//@Configuration
public class InitModelFileds implements BeanPostProcessor{
    /**
     * 此处用反射初始化实体类 , 只用于方便模块代码复用
     * WLJ
     */
    public void changeFileds(Class<?> clazz){
        try {
            Field[] fields =clazz.getDeclaredFields();
            for (Field field : fields) {
                Column annotation =field.getAnnotation(Column.class);
                if(annotation!=null){
                    InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
                    Field name = invocationHandler.getClass().getDeclaredField("memberValues");
                    name.setAccessible(true);
                    Map<String,Object> map = (Map<String, Object>) name.get(invocationHandler);
                    String name1 = String.valueOf(map.get("name"));
                    if(!name1.contains("\"") && !name1.contains("'") && !StringUtils.isEmpty(name1)){
                        map.put("name","\""+name1+"\"");
                    }else{
                        if(name1.contains("'")){
                            map.put("name",name1.replaceAll("'","\""));
                        }
                    }
                    if ("level".equals(name1)) {
                        String value = annotation.name();
                        System.out.println("修改后的值"+value);
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("启动大金仓数据库初始化失败了, 请检查实体类注解@Colomn");
        }
    }
    /*{
        try {
            //Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("application.yml"));
            Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("application.properties"));
            String cluster = properties.getProperty("cluster");
            if("kingbase".equals(cluster)){
                changeFileds(User.class);
                changeFileds(UmsUserInfo.class);
                changeFileds(UserFullView.class);
                changeFileds(UserViewAll.class);
                changeFileds(UmsDepartment.class);
                changeFileds(UmsUserInfoView.class);
                System.out.println("**** 启动大金仓数据库初始化实体类完成 ****");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }*/
    @PostConstruct
    public void init(){
        try {
            //changeFileds(User.class);
            //changeFileds(UmsUserInfo.class);
            //changeFileds(UserFullView.class);
            //changeFileds(UserViewAll.class);
            //changeFileds(UmsDepartment.class);
            //changeFileds(UmsUserInfoView.class);
            System.out.println("**** 启动大金仓数据库初始化实体类完成 ****");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
